"""
通用响应模型
"""
from pydantic import BaseModel, Field
from typing import Optional, Any, Generic, TypeVar

DataT = TypeVar("DataT")


class ApiResponse(BaseModel, Generic[DataT]):
    """
    统一API响应格式
    保持与原Flask项目一致的响应格式
    """
    code: int = Field(default=0, description="状态码 0-成功 1-失败")
    message: str = Field(default="操作成功", description="响应消息")
    data: Optional[DataT] = Field(default=None, description="响应数据")
    
    class Config:
        json_schema_extra = {
            "example": {
                "code": 0,
                "message": "操作成功",
                "data": {}
            }
        }


def success_response(data: Any = None, message: str = "操作成功") -> dict:
    """成功响应"""
    return {
        "code": 0,
        "message": message,
        "data": data
    }


def error_response(message: str = "操作失败", code: int = 1) -> dict:
    """错误响应"""
    return {
        "code": code,
        "message": message,
        "data": None
    }

